package mpc;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;

import Sorters.ISorter;

public class SortHolder extends JPanel {
	
	private ResultDrawer drawArea;
	private JLabel textArea;
	
	private boolean finished = false;
	private String sortName;
	private int place;
	
	private GridBagLayout layout;
	
	
	public SortHolder(String sortName, Sortable[] overallMas, ISorter sorter){
		drawArea = new ResultDrawer(sorter, overallMas);
		this.sortName = sortName;
		textArea = new JLabel(sortName + ". В состоянии работы.");
		//textArea.setMaximumSize(new Dimension(20, 20));
		
		layout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		
		this.setLayout(layout);
		
		c.gridx = 0; c.gridy = 0;
		this.add(drawArea, c);
		c.gridx = 0; c.gridy = 1;
		this.add(textArea, c);
		
		//this.setPreferredSize(new Dimension(MainGUI.mas.length, 200));
		//layout.preferredLayoutSize(this);
		
		drawArea.repaint();
		this.repaint();
	}
	
	public void doNextIteration(){
		if(finished) return;
		finished = drawArea.doNextIteration();
		if(finished){
			place = ++MainGUI.numberOfWinners;
			textArea.setText(sortName + ". Завершена (" + place + ")");
		}
		
		this.repaint();
	}
	
	public boolean isFinished(){
		return finished;
	}
	
	public void attachMas(Sortable[] mas){
		drawArea.attachMas(mas);
		finished = false;
		place = 0;
		textArea.setText(sortName + ". В состоянии работы.");
	}
}
